package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlFieldMappingDtlMapper;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlAddDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlDelDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlDetailDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlEditDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlQueryDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ReqFieldMappingDtlStateDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlAddDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlDelDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlDetailDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlEditDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlQueryDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlStateDTO;
import com.zl.autocode.dto.autocode.fieldMappingDtl.ResFieldMappingDtlVO2;
import com.zl.autocode.entity.autocode.ZlFieldMappingDtl;
import com.zl.autocode.entity.autocode.po.ZlFieldMappingDtlVo;
import com.zl.autocode.service.autocode.IFieldMappingDtlService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 字段映射明细
 * @author ZL
 */
@Service("autocodeFieldMappingDtlService")
public class FieldMappingDtlServiceImpl extends AutocodeBaseManager implements IFieldMappingDtlService {

    @Autowired
    private ZlFieldMappingDtlMapper fieldMappingDtlMapper;

    @Override
    public ResFieldMappingDtlQueryDTO queryFieldMappingDtlList(ReqDTO<ReqFieldMappingDtlQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResFieldMappingDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResFieldMappingDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getMatchingRule())){
            reqData.setMatchingRule("%"+reqData.getMatchingRule()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlFieldMappingDtlVo> list = fieldMappingDtlMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResFieldMappingDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResFieldMappingDtlDetailDTO queryFieldMappingDtlDetail(ReqDTO<ReqFieldMappingDtlDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlDetailDTO reqData = reqInfo.getReqData();
        ResFieldMappingDtlDetailDTO res = new ResFieldMappingDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlFieldMappingDtl view = fieldMappingDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResFieldMappingDtlAddDTO insertFieldMappingDtlInfo(ReqDTO<ReqFieldMappingDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlAddDTO reqData = reqInfo.getReqData();
        ResFieldMappingDtlAddDTO res = new ResFieldMappingDtlAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlFieldMappingDtl record = new ZlFieldMappingDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setMappingDtlId(IdUtil.simpleUUID());
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingDtlId",record.getMappingDtlId());
        //检查主键是否冲突
        if(fieldMappingDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        fieldMappingDtlMapper.insertSelective(record);
        
        ZlFieldMappingDtl keyInfo = fieldMappingDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResFieldMappingDtlEditDTO updateFieldMappingDtlInfo(ReqDTO<ReqFieldMappingDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlEditDTO reqData = reqInfo.getReqData();
        ResFieldMappingDtlEditDTO res = new ResFieldMappingDtlEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlFieldMappingDtl record = new ZlFieldMappingDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        fieldMappingDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingDtlId",record.getMappingDtlId());
        ZlFieldMappingDtl keyInfo = fieldMappingDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResFieldMappingDtlDelDTO deleteFieldMappingDtlInfo(ReqDTO<ReqFieldMappingDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlDelDTO reqData = reqInfo.getReqData();
        ResFieldMappingDtlDelDTO res = new ResFieldMappingDtlDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        fieldMappingDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResFieldMappingDtlStateDTO updateFieldMappingDtlState(ReqDTO<ReqFieldMappingDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDtlStateDTO reqData = reqInfo.getReqData();
        ResFieldMappingDtlStateDTO res = new ResFieldMappingDtlStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlFieldMappingDtl record = new ZlFieldMappingDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        fieldMappingDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingDtlId",record.getMappingDtlId());
        ZlFieldMappingDtl keyInfo = fieldMappingDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}